package com.tongtech.system.controller;

import com.tongtech.common.core.web.controller.BaseController;
import com.tongtech.common.core.web.domain.AjaxResult;
import com.tongtech.common.core.web.page.TableDataInfo;
import com.tongtech.common.log.annotation.Log;
import com.tongtech.common.log.enums.BusinessType;
import com.tongtech.common.security.annotation.RequiresPermissions;
import com.tongtech.common.security.utils.SecurityUtils;
import com.tongtech.system.domain.Customer;
import com.tongtech.system.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 客户信息
 *
 * @author xingyc
 */
@RestController
@RequestMapping("/customer")
public class CustomerController extends BaseController
{
    @Autowired
    private ICustomerService customerService;

    /**
     * 获取客户列表
     */
    @RequiresPermissions("customer:info:list")
    @GetMapping("/list")
    public TableDataInfo list(Customer customer)
    {
        startPage();
        List<Customer> list = customerService.selectCustomerList(customer);
        return getDataTable(list);
    }

    /**
     * 根据客户编号获取详细信息
     */
    @RequiresPermissions("customer:info:query")
    @GetMapping(value = "/{customerId}")
    public AjaxResult getInfo(@PathVariable Long customerId)
    {
        return success(customerService.selectCustomerById(customerId));
    }

    /**
     * 查询C端用户，当前的大客户经理信息
     */
    @RequiresPermissions("customer:project:info")
    @GetMapping(value = "/project/info")
    public AjaxResult getProjectInfo()
    {
        return success(customerService.selectProjectInfo());
    }

    /**
     * 新增客户
     */
    @RequiresPermissions("customer:info:add")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Customer customer)
    {
       if(Objects.nonNull(customer)){
           customer.setCreateBy(SecurityUtils.getUsername());
       }
        return toAjax(customerService.insertCustomer(customer));
    }

    /**
     * 修改客户
     */
    @RequiresPermissions("customer:info:edit")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Customer customer)
    {
        if(Objects.nonNull(customer)){
            customer.setUpdateBy(SecurityUtils.getUsername());
        }
        return toAjax(customerService.updateCustomer(customer));
    }

    /**
     * 删除客户
     */
    @RequiresPermissions("customer:info:remove")
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{customerId}")
    public AjaxResult remove(@PathVariable Long customerId)
    {
        return toAjax(customerService.deleteCustomerById(customerId));
    }
}
